package com.timi_licai.timi_licai_account.utils;

import android.content.Intent;
import android.util.Log;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 *
 */
public class DateUtils {

    public static String LeftPad_Tow_Zero(int str) {
        String ret = null;
        java.text.DecimalFormat format = new java.text.DecimalFormat("00");
        ret = format.format(str);
        return ret;
    }

    public static String format(String format, String time) {
        String result = "";
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
        try {
            Date date = df.parse(time);
            SimpleDateFormat df1 = new SimpleDateFormat(format);
            result = df1.format(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return result;
    }

    public static float getMax(List<Float> list) {
        float ret = 0;
        if (list != null) {
            ret = Collections.max(list);
        }
        return ret;
    }

    public static String parseDate(Date date) {
        String ret = null;
        if (date != null) {
            SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日");
            ret = format.format(date);
        }
        return ret;
    }


    public static Calendar getSelectCalendar(int mPageNumber) {
        Calendar calendar = Calendar.getInstance();
        if (mPageNumber > 500) {
            for (int i = 0; i < mPageNumber - 500; i++) {
                calendar = setNextViewItem(calendar);
            }
        } else if (mPageNumber < 500) {
            for (int i = 0; i < 500 - mPageNumber; i++) {
                calendar = setPrevViewItem(calendar);
            }
        } else {

        }
        return calendar;
    }

    private static Calendar setPrevViewItem(Calendar calendar) {
        int iMonthViewCurrentMonth = calendar.get(Calendar.MONTH);
        int iMonthViewCurrentYear = calendar.get(Calendar.YEAR);
        iMonthViewCurrentMonth--;// 当前选择月--

        // 如果当前月为负数的话显示上一年
        if (iMonthViewCurrentMonth == -1) {
            iMonthViewCurrentMonth = 11;
            iMonthViewCurrentYear--;
        }
        calendar.set(Calendar.DAY_OF_MONTH, 1); // 设置日为当月1日
        calendar.set(Calendar.MONTH, iMonthViewCurrentMonth); // 设置月
        calendar.set(Calendar.YEAR, iMonthViewCurrentYear); // 设置年
        return calendar;
    }

    private static Calendar setNextViewItem(Calendar calendar) {
        int iMonthViewCurrentMonth = calendar.get(Calendar.MONTH);
        int iMonthViewCurrentYear = calendar.get(Calendar.YEAR);
        iMonthViewCurrentMonth++;
        if (iMonthViewCurrentMonth == 12) {
            iMonthViewCurrentMonth = 0;
            iMonthViewCurrentYear++;
        }
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.set(Calendar.MONTH, iMonthViewCurrentMonth);
        calendar.set(Calendar.YEAR, iMonthViewCurrentYear);
        return calendar;
    }

    /**
     * 将一个Date类型装换成可以在数据库中存储的类型
     *
     * @param date
     * @return
     */
    public static String formatDateForDatabase(Date date) {
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
        String dateFormat = format.format(date);
        return dateFormat;
    }
}
